package edu.ynmd.database.action;

import edu.ynmd.database.dao.AreaDao;
import edu.ynmd.database.dao.CityDao;
import edu.ynmd.database.dao.ProvinceDao;
import edu.ynmd.database.dao.StudentDao;
import edu.ynmd.database.model.StudentmdEntity;
import edu.ynmd.database.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
public class PublicAction {
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ProvinceDao provinceDao;

    @Autowired
    private CityDao cityDao;

    @Autowired
    private AreaDao areaDao;


    @GetMapping("getAnObject")
    @ResponseBody
    public StudentmdEntity getAnObject() throws Exception{

        List<StudentmdEntity> sl=studentDao.findAll();
        if(sl.size()>0){
            return  sl.get(0);
        }
        else{
            return null;
        }


    }



    @PostMapping("SaveStudentInfo")
    @ResponseBody
    public Map saveStudentInfo(@RequestBody StudentVo studentVo) throws Exception{
        Map m=new HashMap();

        String sf=studentVo.getSfzh().substring(0,2);
        String sfname=provinceDao.getProvinceNameBySfCode(sf+"0000");

        String zs=studentVo.getSfzh().substring(2,4);
        String zsname=cityDao.getCityNameByZsCode(sf+zs+"00");

        String qx=studentVo.getSfzh().substring(4,6);
        String qxname=areaDao.getAreaNameByQxCode(sf+zs+qx);

        String csrq=studentVo.getSfzh().substring(6,14);
        Timestamp csrqtime=this.getBirthTimeByStr(csrq);

        String xb=studentVo.getSfzh().substring(16,17);
        String xbname=this.getXbByCode(xb);

        //待保存到数据库的对象
        StudentmdEntity studentmdEntity=new StudentmdEntity();
        if (studentVo.getStudentid()!=null&&!studentVo.getStudentid().equals("")){
            studentmdEntity.setStudentid(studentVo.getStudentid());
        }
        studentmdEntity.setNativeplace(sfname+zsname+qxname);
        studentmdEntity.setBirthday(csrqtime);
        studentmdEntity.setSex(xbname);
        studentmdEntity.setName(studentVo.getXm());
        studentmdEntity.setStunum(studentVo.getXh());

        studentDao.save(studentmdEntity);


        m.put("province",sfname);
        m.put("city",zsname);
        m.put("area",qxname);
        m.put("csrq",csrqtime);
        m.put("xb",xbname);


        return m;
    }

    private String getXbByCode(String code){
        int sex=Integer.parseInt(code);
        if(sex%2==0){
            return "女";
        }
        else {
            return "男";
        }
    }

    private Timestamp getBirthTimeByStr(String birthStr){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(birthStr, pos);
        return new Timestamp(strtodate.getTime());
    }


    @GetMapping("getStudentList")
    @ResponseBody
    public List<StudentmdEntity>getStudentList()throws Exception{
        List<StudentmdEntity> sl=studentDao.findAll();
        return sl;

    }

    @PostMapping("getSingleStudentById")
    @ResponseBody
    public StudentmdEntity getSingleStudentById(@RequestBody StudentVo studentVo)throws Exception{

        StudentmdEntity studentmdEntity=studentDao.findOne(studentVo.getStudentid());
        return studentmdEntity;
    }

    @PostMapping("deleteStudent")
    @ResponseBody
    public boolean deleteStudent(@RequestBody StudentVo studentVo) throws Exception{
        try {
            studentDao.delete(studentVo.getStudentid());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
